<template>
  <div class="order-detail">
    <div class="page-header">
      <div class="header-content">
        <el-button @click="goBackToOrders">返回订单列表</el-button>
        <h1>订单详情</h1>
      </div>
    </div>

    <div class="detail-content" v-loading="loading">
      <el-card class="order-section">
        <template #header>
          <div class="section-header">
            <h2>订单信息</h2>
            <el-tag :type="getStatusType">{{ getStatusText }}</el-tag>
          </div>
        </template>
        
        <div class="info-list">
          <div class="info-item">
            <span class="label">订单编号：</span>
            <span class="value">{{ order.id }}</span>
          </div>
          <div class="info-item">
            <span class="label">创建时间：</span>
            <span class="value">{{ formatDate(order.createdAt) }}</span>
          </div>
          <div class="info-item">
            <span class="label">支付方式：</span>
            <span class="value">{{ getPaymentMethodText }}</span>
          </div>
        </div>
      </el-card>

      <el-card class="order-section">
        <template #header>
          <div class="section-header">
            <h2>收货信息</h2>
          </div>
        </template>
        
        <div class="info-list">
          <div class="info-item">
            <span class="label">收货人：</span>
            <span class="value">{{ order.shippingAddress?.name }}</span>
          </div>
          <div class="info-item">
            <span class="label">联系电话：</span>
            <span class="value">{{ order.shippingAddress?.phone }}</span>
          </div>
          <div class="info-item">
            <span class="label">收货地址：</span>
            <span class="value">{{ order.shippingAddress?.address }}</span>
          </div>
        </div>
      </el-card>

      <el-card class="order-section">
        <template #header>
          <div class="section-header">
            <h2>商品信息</h2>
          </div>
        </template>

        <div class="order-items">
          <div v-for="item in order.items" :key="item.id" class="order-item">
            <div class="item-image">
              <img :src="item.image" :alt="item.name" />
            </div>
            <div class="item-info">
              <h3>{{ item.name }}</h3>
              <p class="price">¥{{ item.price.toFixed(2) }} × {{ item.quantity }}</p>
            </div>
            <div class="item-subtotal">
              ¥{{ (item.price * item.quantity).toFixed(2) }}
            </div>
          </div>
        </div>

        <div class="order-summary">
          <div class="summary-item">
            <span>商品总额：</span>
            <span class="amount">¥{{ ((order.total || 0) - (order.shipping || 0)).toFixed(2) }}</span>
          </div>
          <div class="summary-item">
            <span>运费：</span>
            <span class="amount">¥{{ (order.shipping || 0).toFixed(2) }}</span>
          </div>
          <div class="summary-item total">
            <span>实付金额：</span>
            <span class="amount">¥{{ (order.total || 0).toFixed(2) }}</span>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'OrderDetail',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const loading = ref(false)
    const order = ref({})

    const getStatusType = computed(() => {
      const statusMap = {
        pending: 'warning',
        paid: 'success',
        shipped: 'primary',
        completed: 'success',
        cancelled: 'info'
      }
      return statusMap[order.value.status] || 'info'
    })

    const getStatusText = computed(() => {
      const statusMap = {
        pending: '待付款',
        paid: '已付款',
        shipped: '已发货',
        completed: '已完成',
        cancelled: '已取消'
      }
      return statusMap[order.value.status] || '未知状态'
    })

    const getPaymentMethodText = computed(() => {
      const methodMap = {
        alipay: '支付宝',
        wechat: '微信支付'
      }
      return methodMap[order.value.paymentMethod] || '未知方式'
    })

    const formatDate = (dateString) => {
      const date = new Date(dateString)
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false
      })
    }

    const loadOrder = async () => {
      loading.value = true
      try {
        const response = await axios.get(`http://localhost:3000/orders/${route.params.id}`)
        order.value = response.data
      } catch (error) {
        console.error('Error loading order:', error)
        ElMessage.error('加载订单信息失败')
        router.push('/orders')
      } finally {
        loading.value = false
      }
    }

    const goBackToOrders = () => {
      router.push({ path: '/medicines', query: { tab: 'orders' } })
    }

    onMounted(() => {
      loadOrder()
    })

    return {
      loading,
      order,
      getStatusType,
      getStatusText,
      getPaymentMethodText,
      formatDate,
      goBackToOrders
    }
  }
}
</script>

<style scoped>
.order-detail {
  padding: 20px;
  max-width: 1000px;
  margin: 0 auto;
}

.page-header {
  margin-bottom: 30px;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 20px;
}

.header-content h1 {
  margin: 0;
  font-size: 24px;
  color: #2c3e50;
}

.order-section {
  margin-bottom: 20px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.section-header h2 {
  margin: 0;
  font-size: 18px;
  color: #2c3e50;
}

.info-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.info-item {
  display: flex;
  align-items: center;
}

.info-item .label {
  width: 100px;
  color: #606266;
}

.info-item .value {
  color: #2c3e50;
}

.order-items {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.order-item {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 0;
  border-bottom: 1px solid #ebeef5;
}

.order-item:last-child {
  border-bottom: none;
}

.item-image {
  width: 80px;
  height: 80px;
  border-radius: 4px;
  overflow: hidden;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-info {
  flex: 1;
}

.item-info h3 {
  margin: 0 0 8px;
  font-size: 16px;
  color: #2c3e50;
}

.item-info .price {
  margin: 0;
  color: #606266;
}

.item-subtotal {
  font-size: 16px;
  color: #f56c6c;
  font-weight: 500;
}

.order-summary {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.summary-item {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 10px;
  color: #606266;
}

.summary-item:last-child {
  margin-bottom: 0;
}

.summary-item.total {
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px solid #dcdfe6;
  color: #303133;
  font-weight: 500;
}

.summary-item .amount {
  margin-left: 20px;
  color: #f56c6c;
  font-weight: 500;
}
</style> 